<?php
// application/bootstrap.php
//
// Step 1: APPLICATION CONSTANTS - Set the constants to use in this application.
// These constants are accessible throughout the application, even in ini
// files. We optionally set APPLICATION_PATH here in case our entry point
// isn't index.php (e.g., if required from our test suite or a script).

require_once('_config.local.php');

defined('APPLICATION_PATH')
    or define('APPLICATION_PATH', dirname(__FILE__));

defined('APPLICATION_ENVIRONMENT')
    or define('APPLICATION_ENVIRONMENT', 'development');

defined('APPLICATION_STARTURL')
    or define('APPLICATION_STARTURL', '/');

$config = new Zend_Config_Ini(APPLICATION_PATH.'/config.ini', 'general');
$registry = Zend_Registry::getInstance();
$registry->set('config', $config);
// Setup the database
$db = Zend_Db::factory($config->db->adapter, $config->db->config->toArray());
$db->query('SET NAMES utf8');
Zend_Db_Table::setDefaultAdapter($db);
$debug = 0;
if (preg_match('/DEBUG/', $_SERVER['HTTP_USER_AGENT'])) {
	$debug = 3;
}
// First, set up the Cache
$frontendOptions = array(
	'automatic_serialization' => true
);

$backendOptions  = array(
    'cache_dir' => PATH . '/cache'
);

$cache = Zend_Cache::factory('Core',
                             'File',
                             $frontendOptions,
                             $backendOptions);


// Next, set the cache to be used with all table objects
Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);

$registry->set('db', $db);
/*
 * Firebug debug
 *
 * Install Firebug and FirePHP in Firefox to get this working
 *
 * Firebug: https://addons.mozilla.org/nl/firefox/addon/1843
 * FirePHP: https://addons.mozilla.org/nl/firefox/addon/6149
 *
 * Usage example:
 *
 *  Zend_Registry::get( 'logger' )->info( 'This is an info message' );
 *  Zend_Registry::get( 'logger' )->warning( 'This is a warning message' );
 *  Zend_Registry::get( 'logger' )->error( 'This is an error message' );
 *
 */
$writer = new Zend_Log_Writer_Firebug();
$logger = new Zend_Log( $writer );
$filter = new Zend_Log_Filter_Suppress();

// Enable / disable debugging based on debug variable
if( $debug <= 0 ) {
	$filter->suppress( true );
} elseif( $debug >= 0 ) {
	$filter->accept( true );
}

// Add the filter to the logger and add the logger to the registry
$logger->addFilter( $filter );
$registry->set( 'logger', $logger );

// Add Zend_DB_Table query logging to debug from debug level 3 and up
if( $debug >= 3 ) {
	$profiler = new Zend_Db_Profiler_Firebug( 'DEFAULT' );
	$profiler->setEnabled( true );
	$db->setProfiler( $profiler );
}


$debugInst = General_Debug::getInstance();
$registry->set('debug', $debugInst);


Zend_Layout::startMvc();
// Step 2: FRONT CONTROLLER - Get the front controller.
// The Zend_Front_Controller class implements the Singleton pattern, which is a
// design pattern used to ensure there is only one instance of
// Zend_Front_Controller created on each request.
$frontController = Zend_Controller_Front::getInstance();

// Step 3: CONTROLLER DIRECTORY SETUP - Point the front controller to your action
// controller directory.
$frontController->addModuleDirectory(APPLICATION_PATH.'/modules');

// Step 4: APPLICATION ENVIRONMENT - Set the current environment.
// Set a variable in the front controller indicating the current environment --
// commonly one of development, staging, testing, production, but wholly
// dependent on your organization's and/or site's needs.
$frontController->setParam('env', APPLICATION_ENVIRONMENT);


$frontController->throwExceptions(true);

// Step 5: DISPATCH:  Dispatch the request using the front controller.
// The front controller is a singleton, and should be setup by now. We
// will grab an instance and call dispatch() on it, which dispatches the
// current request.
$frontController->dispatch();

// Step 6: CLEANUP - Remove items from global scope.
// This will clear all our local boostrap variables from the global scope of
// this script (and any scripts that called bootstrap).  This will enforce
// object retrieval through the applications's registry.
unset($frontController);
